#pragma once

#include <QTcpSocket>
#include <QDir>
#include <QStandardPaths>
#include <QListWidget>

// Qt Xml
#include <QDomDocument>
#include <QXmlStreamWriter>

#include "Util/socketqueue.h"
#include "clientinfo.h"
#include "Util/sendfilewidget.h"

class TcpSocket : public QObject
{
    Q_OBJECT
public:
    explicit TcpSocket(QTcpSocket *client, ClientInfo *infoWidget, QListWidget *listWidget);
    ~TcpSocket() override;

    QTcpSocket *client;
    ClientInfo *infoWidget;
    QListWidget *listWidget;

    void writeFileList(const QString &path);
    void writeSendFileRequest(const QString &serverPath, const QString &clientPath);
    void parseReceiveMsg(const QByteArray &array);

    void addSocketQueue(SocketQueue *sq);
    void writeFirstSocketQueue();

private slots:
    void onReadyRead();
    void onBytesWritten();

private:
    QList<SocketQueue*> mSqlist;
    QByteArray mBuffer;

    struct FileWidgetItem
    {
        SendFileWidget *widget;
        QListWidgetItem *item;
        friend bool operator<(const FileWidgetItem &a, const FileWidgetItem &b) {
            if(a.widget != b.widget)
                return a.widget < b.widget;
            return a.item < b.item;
        }
    };
    QMap<QString, QCryptographicHash*> mMapFileChecksum;
    QMap<QString, FileWidgetItem> mMapFileWidgetItem;
};
